<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../../../assets/img/shangjia.png" />
      </div>

      <!-- 登录表单区域 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        label-width="0px"
        class="login_form"
      >
        <!-- 用户名 -->
        <el-form-item>
          <el-input
            v-model="loginForm.merchantId"
            prefix-icon="iconfont icon-user"
            placeholder="请输入商家账号"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item>
          <el-input
            v-model="loginForm.merchantPassword"
            prefix-icon="iconfont icon-3702mima"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="warning" @click="Login" plain>登录</el-button>
          <el-button @click="Regist" plain>注册</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 注册页面dialog -->

    <el-dialog
      title="注册账户"
      :visible.sync="setRegistDialogVisible"
      width="50%"
      @close="setRegistDialogClose"
      :close-on-click-modal="false"
    >
      <el-form :model="registerForm" ref="registruleForm" label-width="100px">
        <el-form-item label="商家账号">
          <el-input
            placeholder="字母开头，4-15位，允许字母数字下划线"
            v-model="registerForm.merchantId"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input
            type="password"
            show-password
            v-model="registerForm.merchantPassword"
          ></el-input>
        </el-form-item>

        <el-form-item label="确认密码">
          <el-input
            type="password"
            show-password
            v-model="registerForm.merchantRePassword"
          ></el-input>
        </el-form-item>

        <el-form-item label="商家名称">
          <el-input v-model="registerForm.merchantName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <!-- 后期修注册发送请求 -->
        <!-- 点击确认注册按钮 -->
        <el-button type="primary" @click="setRegist">确 定</el-button>
        <el-button @click="setRegistDialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "LoginMerchant",
  data() {
    // // 确认密码的验证规则
    // let rePasswordRules = (rule, value, callback) => {
    //   if (value !== this.registerForm.merchantPassword) {
    //     callback(new Error("两次输入密码不一致!"));
    //   } else {
    //     return callback();
    //   }
    // };

    // // 验证商家账号的规则(字母开头，允许4-15字节，允许字母数字下划线)
    // let checkMerchantId = (rule,value,cb)=>{
    //   const regMerchantId = /^[a-zA-Z][a-zA-Z0-9_]{4,15}$/
    //   if(regMerchantId.test(value)){
    //     return cb()
    //   }
    //   cb(new Error('请输入合法的账号'))
    // }

    return {
      //登录表单的数据绑定对象
      loginForm: {
        merchantId: "",
        merchantPassword: "",
      },

      // 用户注册的数据绑定对象
      registerForm: {
        merchantId: "",
        merchantPassword: "",
        merchantRePassword: "",
        merchantName: "",
      },

      //注册弹出框显示的控制
      setRegistDialogVisible: false,

      // // 表单验证规则对象
      // loginFormRules: {
      //   // 验证用户名是否合法
      //   merchantId: [
      //     { required: true, message: "请输入您的账户", trigger: "blur" },
      //     {
      //       validator:checkMerchantId,
      //       trigger: "blur",
      //       message:"商家账户错误"
      //     },
      //   ],
      //   // 验证密码是否合法
      //   merchantPassword: [
      //     { required: true, message: "请输入密码", trigger: "blur" },
      //     {
      //       min: 6,
      //       max: 20,
      //       message: "长度在 6 到 20 个字符",
      //       trigger: "blur",
      //     },
      //   ],
      // },

      // //用户注册的表单验证对象
      // registUserRules: {
      //   merchantId: [
      //     {
      //       required: true,
      //       message: "请输入您的昵称",
      //       trigger: "blur",
      //     },
      //     {
      //       min: 4,
      //       max: 15,
      //       message: "长度在 4 到 15 个字符",
      //       trigger: "blur",
      //     },
      //     {
      //       validator:checkMerchantId,
      //       trigger: "blur"
      //     }
      //   ],
      //   merchantPassword: [
      //     {
      //       required: true,
      //       message: "请输入密码",
      //       trigger: "blur",
      //     },
      //     {
      //       min: 3,
      //       max: 10,
      //       message: "长度在 3 到 10 个字符",
      //       trigger: "blur",
      //     },
      //   ],
      //    // 验证确认密码
      //   merchantRePassword: [
      //     { required: true, message: "请输入密码", trigger: "blur" },
      //     { validator: rePasswordRules, trigger: "blur" },
      //   ],
      // },
    };
  },
  methods: {
    // 登录
    Login() {
      this.$router.push("/merchanthome");
    },
    // 注册
    Regist() {
      // 控制注册对话框的显示与隐藏
      this.setRegistDialogVisible = true;
    },
    // 取消注册时的注册
    setRegistDialogClose() {
      this.$refs.registruleForm.resetFields();
    },
    // 点击确定注册的按钮
    setRegist() {
      this.setRegistDialogVisible = false;
      // 验证注册对象并发送网络请求
      // 失败 return
      // 成功保存到数据库中并给予token
      this.$message.success("您注册成功");
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #FF9966;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  // 居中语法
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    // 父元素的高度
    transform: translate(-50%, -50%);
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  // 怪异盒模型防止padding
  box-sizing: border-box;
}
.btns {
  display: flex;
  // 居右对齐
  justify-content: flex-end;
}
</style>